package com.example.administrator.easy.view;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ListView;

import com.example.administrator.easy.R;
import com.example.administrator.easy.adapter.ShopAdapter;
import com.example.administrator.easy.detailxinfo.DetailBean;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/11/13.
 */

public class ShangFragment extends Fragment {

    private String urlTemp="http://m.ieasy.tv/api/article/list/44/1/20/00";
    private ListView listView;
    private List<DetailBean> list;
    private ShopAdapter adapter;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_shang,null);

        initvew(view);
        initData();
        setData();
        intiheadview();
        return  view;
    }
//
    private void intiheadview() {
        View view=View.inflate(getActivity(),R.layout.shop_head,null);
        view.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        listView.addHeaderView(view);
    }

    private void setData() {
        listView.setAdapter(adapter);
    }

    private void initData() {
        list=new ArrayList<>();
        adapter=new ShopAdapter(list,getActivity());

        RequestParams params=new RequestParams(urlTemp);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject object=new JSONObject(result);
                    JSONArray array=object.optJSONArray("result");

                    for (int i = 0; i < array.length(); i++) {
                        DetailBean bean=new DetailBean();
                        JSONObject object1 = array.optJSONObject(i);
                        String content = object1.optString("Content");
                        String ico = object1.optString("Ico");
                        String title = object1.optString("Title");
                        int articleId = object1.optInt("ArticleId");
                        bean.userId=articleId;
                        bean.count=content;
                        bean.urlCount=ico;
                        bean.userName=title;
                        list.add(bean);

                    }
                    adapter.notifyDataSetChanged();




                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void initvew(View v) {
        listView= (ListView) v.findViewById(R.id.shop_lv);
    }
}
